﻿using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using RearLivestock.Beginning.Api.Application.Command.Farrowing;
using RearLivestock.Beginning.Api.Application.Command.FarrowingsTable;
using RearLivestock.Common.Dtos;
using NPOI;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using NPOI.SS.Util;
using Microsoft.AspNetCore.Http.HttpResults;
using NPOI.HSSF.UserModel;
using RearLivestock.Common.Domains.hhx;

namespace RearLivestock.Beginning.Api.Controllers
{
    /// <summary>
    /// 母猪分娩控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class FarrowingsController : ControllerBase
    {
        private readonly IMediator mediator;

        public FarrowingsController(IMediator mediator)
        {
            this.mediator = mediator;
        }

        /// <summary>
        /// 分娩难易列表
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> GetDifficulties(GetDifficultiesCommand cmd)
        {
            return await mediator.Send(cmd);
        }
        /// <summary>
        /// 肥猪批次列表
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> GetStudies(GetBatchsCommand cmd)
        {
            return await mediator.Send(cmd);
        }
        /// <summary>
        /// 母性批次
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> GetMaternals(GetMaternalsCommand cmd)
        {
            return await mediator.Send(cmd);
        }
        /// <summary>
        /// 母猪分娩添加
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> AddFarrowings(AddFarrowingsCommmand cmd)
        {
            return await mediator.Send(cmd);
        }
        /// <summary>
        /// 母猪分娩列表
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result<PageResultDto<FarrowingsDto>>> ShowFarrowings(QueryFarrowingsCommand cmd)
        {
            return await mediator.Send(cmd);
        }
        /// <summary>
        /// 母猪分娩删除
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<Result> DeleteFarrowings(DeleteFarrowingsCommand cmd)
        {
            return await mediator.Send(cmd);
        }
        /// <summary>
        /// 母猪分娩详情获取
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result<FarrowingsDto>> GetFarrowings(GetFarrowingsCommand cmd)
        {
            return await mediator.Send(cmd);
        }
        /// <summary>
        /// 母猪分娩修改
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<Result> UpdateFarrowings(UpdateFarrowingsCommand cmd)
        {
            return await mediator.Send(cmd);
        }
        /// <summary>
        /// 母猪分娩列表导出
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> ExportFarrowingsExcel([FromQuery] ExcelFarrowingsCommand cmd)
        {
            var res = await mediator.Send(cmd);
            return File(res.FileBytes, res.MimeType, res.FileName);
        }

        /// <summary>
        /// 导入母猪分娩信息
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> ImportFarrowings(ImportFarrowingCommand cmd)
        {
            return await mediator.Send(cmd);
        }
    }
}
